// Definitions of the basic macros
//

#ifndef __MACROS_H__
#define __MACROS_H__

#include "Types.h"

/////////////////////////////////////////////////////////////////////////////
// for macro functions

#define MACRO_START	do
#define MACRO_STOP	while(0)

/////////////////////////////////////////////////////////////////////////////
// Bit-wise operations

/* Set n the specific bit to the value */
#define	SET_BIT(n,bit,val)	((n) = (((n) & (~(bit))) | ((bit) * (val))))

/* Get the specific bit of n */
#define GET_BIT(n,bit)		((n) & (bit))

/* Toggle the the specific bit of n */
#define TOGGLE_BIT(n,bit)	(((n) ^= (bit)) & (bit))

/* Swap the value of two variables */
#define SWAP(a,b)			((a) ^= (b) ^= (a) ^= (b))


/////////////////////////////////////////////////////////////////////////////
// Mathmatic operations

#define ABS(a)				((a) < 0 ? -(a) : (a))

#define ABS_SUB(a, b)		((a) > (b) ? ((a)-(b)) : ((b)-(a)))

#define SIGN(a)				(((a) < 0) ? -1 : 1)

#ifndef max
#define max(a,b)			(((a) > (b)) ? (a) : (b))
#endif

#ifndef min
#define min(a,b)			(((a) < (b)) ? (a) : (b))
#endif


/////////////////////////////////////////////////////////////////////////////
// Miscellaneous helper functions

#ifdef __cplusplus
#define SAFE_DELETE(p)			{ if (p) { delete (p);     (p) = NULL; } }
#define SAFE_DELETE_ARRAY(p)	{ if (p) { delete[] (p);   (p) = NULL; } }
#define SAFE_RELEASE(p)			{ if (p) { (p)->Release(); (p) = NULL; } }
#else
#define SAFE_FREE(p)			{ if (p) { ut_free(p); (p) = NULL; } }
#endif

#define RECT_WIDTH(rt)			((rt).right - (rt).left + 1)

#define RECT_HEIGHT(rt)			((rt).bottom - (rt).top + 1)

#define SAME_RECT(rt1, rt2)		((rt1).left == (rt2).left && (rt1).top == (rt2).top && \
								 (rt1).right == (rt2).right && (rt1).bottom == (rt2).bottom )

#define PT_IN_RECT(rt,p)		((p).x >= (rt).left && (p).x <= (rt).right && \
								 (p).x >= (rt).top && (p).y <= (rt).bottom)

#define PT_NOT_IN_RECT(rt,p)	((p).x < (rt).left || (p).x > (rt).right || \
								 (p).x < (rt).top || (p).y > (rt).bottom)

#endif	// __MACROS_H__
